<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/10/9
 * Time: 9:39
 */
namespace app\portal\controller;

use app\portal\model\PortalPostModel;
use cmf\controller\AdminBaseController;
use app\admin\model\ThemeModel;
use think\Db;
class AdminVideoController extends AdminBaseController{
    public function index(){
        $articles=Db::name("video")->where("post_status",1)->paginate(10);

        $this->assign('articles',$articles);
        return $this->fetch();
    }
    public function add(){
        return $this->fetch();
    }
    public function addPost(){
        $data   = $this->request->param();
        $post=$data['post'];
        $post['post_url']=$data['file_urls'][0];
        $post['more']=$data['post']['more']['thumbnail'];
        $result=Db::name("video")->insert($post);

        if ($result){
            $this->success('添加成功!', url('AdminVideo/index'));
        }
    }
    public function edit(){
        $data   = $this->request->param();
        $post=Db::name("video")->where($data)->find();
        $this->assign('post',$post);
        return $this->fetch();
    }
    public function editPost(){
        $data   = $this->request->param();
        $post=$data['post'];
        $post['post_url']=$data['file_urls'][0];
        $post['more']=$data['post']['more']['thumbnail'];
        $result=Db::name("video")->where('id',$post['id'])->update($post);
        if ($result){
            $this->success('更新成功!', url('AdminVideo/index'));
        }
    }
    public function delete()
    {
        $param           = $this->request->param();
        if (isset($param['id'])) {
            $id           = $this->request->param('id', 0, 'intval');
            $result=Db::name("video")->where('id',$id)->delete();
            if ($result){
                $this->success('删除成功!', url('AdminVideo/index'));
            }
        }
    }
    public function publish()
    {
        $param           = $this->request->param();
        $portalPostModel = new PortalPostModel();

        if (isset($param['ids']) && isset($param["yes"])) {
            $ids = $this->request->param('ids/a');

            $portalPostModel->where(['id' => ['in', $ids]])->update(['post_status' => 1, 'published_time' => time()]);

            $this->success("发布成功！", '');
        }

        if (isset($param['ids']) && isset($param["no"])) {
            $ids = $this->request->param('ids/a');

            $portalPostModel->where(['id' => ['in', $ids]])->update(['post_status' => 0]);

            $this->success("取消发布成功！", '');
        }

    }

    /**
     * 文章置顶
     * @adminMenu(
     *     'name'   => '文章置顶',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '文章置顶',
     *     'param'  => ''
     * )
     */
    public function top()
    {
        $param           = $this->request->param();


        if (isset($param['ids']) && isset($param["yes"])) {
            $ids = $this->request->param('ids/a');

            Db::name("video") ->where(['id' => ['in', $ids]])->update(['is_top' => 1]);

            $this->success("置顶成功！", '');

        }

        if (isset($_POST['ids']) && isset($param["no"])) {
            $ids = $this->request->param('ids/a');

            Db::name("video")->where(['id' => ['in', $ids]])->update(['is_top' => 0]);

            $this->success("取消置顶成功！", '');
        }
    }

    /**
     * 文章推荐
     * @adminMenu(
     *     'name'   => '文章推荐',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '文章推荐',
     *     'param'  => ''
     * )
     */
    public function recommend()
    {
        $param           = $this->request->param();


        if (isset($param['ids']) && isset($param["yes"])) {
            $ids = $this->request->param('ids/a');

            Db::name("video")->where(['id' => ['in', $ids]])->update(['recommended' => 1]);

            $this->success("推荐成功！", '');

        }
        if (isset($param['ids']) && isset($param["no"])) {
            $ids = $this->request->param('ids/a');

            Db::name("video")->where(['id' => ['in', $ids]])->update(['recommended' => 0]);

            $this->success("取消推荐成功！", '');

        }
    }

}